package md.frolov.bible.editors;

import md.frolov.bible.model.IURLProvider;
import md.frolov.bible.toolbars.NavigationTextContribution;
import md.frolov.bible.views.BooksView;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorPart;

public class MainEditor extends EditorPart {
	private BibleControl myBibleControl = null;
	
	public static final String PLUGIN_ID = "md.frolov.bible.editors.MainEditor";
	
	public MainEditor() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)throws PartInitException {
		setSite(site);
		setInput(input);
		setPartName(input.getName());
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void setBibleInput(BibleEditorInput bei){
		setInput(bei);
		update();
	}
	
	public void update(){
		BibleEditorInput bei = (BibleEditorInput)getEditorInput();
		myBibleControl.setContents(bei);
		setPartName(bei.getName());
		NavigationTextContribution.refresh();
	}

	@Override
	public void createPartControl(Composite parent) {
		FillLayout layout = new FillLayout();
		parent.setLayout(layout);
		
//		StackLayout sl = new StackLayout();
//		parent.setLayout(sl);
		
//		GridLayout gd = new GridLayout(1,false);
//		parent.setLayout(gd);
		
//		Text t = new Text(parent, SWT.BORDER);
//		t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		myBibleControl = new BibleControl(parent, SWT.NONE);
		getSite().setSelectionProvider(myBibleControl);
//		myBibleControl.setLayoutData(new GridData(GridData.FILL_BOTH));
		update();
	
	}
	
	private void hookLocationListener(){
/*		browser.addLocationListener(new LocationListener(){

			@Override
			public void changed(LocationEvent event) {
//				System.out.println("Location changed: "+event.location);
				ReferenceView rv = (ReferenceView) Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ReferenceView.PLUGIN_ID);
				rv.setInput(new BibleVerseIdInput(event.location));
				event.doit = false;
			}

			@Override
			public void changing(LocationEvent event) {
//				System.out.println("Location change: "+event.location);
//				event.doit = false;
			}
			
		});
*/	}
	
	private void hookBookSelectionListener(){
		getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(BooksView.PLUGIN_ID, new ISelectionListener(){
			@Override
			public void selectionChanged(IWorkbenchPart part, ISelection selection) {
				
				
				if(!(part instanceof BooksView)) return;
				if(!getSite().getPage().getActiveEditor().equals(MainEditor.this)) return;
				if(selection==null) return;
				if(!(selection instanceof IStructuredSelection)) return;
				Object objSelection = ((IStructuredSelection) selection).getFirstElement();
				if(objSelection == null) return;
				if(!(objSelection instanceof IURLProvider)) return;
				
				BooksView bv = (BooksView) part.getSite().getPage().findView(BooksView.PLUGIN_ID);
				EditorPart ep = (EditorPart) part.getSite().getPage().getActiveEditor();
				System.out.println("noPostSelection: "+bv.getViewer().getData("noPostSelection")+", selection: "+selection+" ep:"+ep);
				
				if(bv.getViewer().getData("noPostSelection")!=null) {
					return;
				}
				
				String newURL = ((IURLProvider)objSelection).getUrl();
				if(((BibleEditorInput)getEditorInput()).getChapter().getUrl().equals(newURL)) return;
				
				setBibleInput(new BibleEditorInput(newURL));
			}
			
		});
	}

	@Override
	public void setFocus() {
		NavigationTextContribution.refresh();
		myBibleControl.setFocus();
	}

}
